home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / GWMALLOC.ZIP;1 / GWMALLOC.TAR / gw_malloc / heap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-08  |  2.7 KB  |  119 lines

  1. /*
  2.  * system specific memory routines
  3.  *
  4.  * Copyright 1992 by Gray Watson and the Antaire Corporation
  5.  *
  6.  * This file is part of the malloc-debug package.
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library (see COPYING-LIB); if not, write to the
  20.  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * The author of the program may be contacted at gray.watson@antaire.com
  23.  */
  24.  
  25. /*
  26.  * These are the system/machine specific routines for allocating space on the
  27.  * heap as well as reporting the current position of the heap.
  28.  */
  29.  
  30. #define MALLOC_DEBUG_DISABLE
  31.  
  32. #include "malloc.h"
  33. #include "malloc_loc.h"
  34.  
  35. #include "chunk.h"
  36. #include "compat.h"
  37. #include "conf.h"
  38. #include "error.h"
  39. #include "error_val.h"
  40. #include "heap.h"
  41.  
  42. #if INCLUDE_RCS_IDS
  43. LOCAL    char    *rcs_id =
  44.   "$Id: heap.c,v 1.18 1993/04/05 01:29:03 gray Exp $";
  45. #endif
  46.  
  47. /* external routines */
  48. #if HAVE_SBRK
  49. IMPORT    char        *sbrk(int incr);    /* to extend the heap */
  50. #define SBRK_ERROR    ((char *)-1)        /* sbrk error code */
  51. #endif
  52.  
  53. /* exported variables */
  54. EXPORT    char        *_heap_base = NULL;    /* base of our heap */
  55. EXPORT    char        *_heap_last = NULL;    /* end of our heap */
  56.  
  57. /*
  58.  * function to get SIZE memory bytes from the end of the heap
  59.  */
  60. EXPORT    char    *_heap_alloc(unsigned int size)
  61. {
  62.   char        *ret = HEAP_ALLOC_ERROR;
  63.   
  64. #if HAVE_SBRK
  65.   ret = sbrk(size);
  66.   if (ret == SBRK_ERROR) {
  67.     malloc_errno = MALLOC_ALLOC_FAILED;
  68.     _malloc_perror("_heap_alloc");
  69.     ret = HEAP_ALLOC_ERROR;
  70.   }
  71.   else {
  72.     /* increment last pointer */
  73.     _heap_last += size;
  74.   }
  75. #endif
  76.   
  77.   return ret;
  78. }
  79.  
  80. /*
  81.  * return a pointer to the current end of the heap
  82.  */
  83. EXPORT    char    *_heap_end(void)
  84. {
  85.   char        *ret = HEAP_ALLOC_ERROR;
  86.   
  87. #if HAVE_SBRK
  88.   ret = sbrk(0);
  89.   if (ret == SBRK_ERROR) {
  90.     malloc_errno = MALLOC_ALLOC_FAILED;
  91.     _malloc_perror("_heap_end");
  92.     ret = HEAP_ALLOC_ERROR;
  93.   }
  94. #endif
  95.   
  96.   return ret;
  97. }
  98.  
  99. /*
  100.  * initialize heap pointers
  101.  */
  102. EXPORT    void    _heap_startup(void)
  103. {
  104.   _heap_base = _heap_last = _heap_end();
  105. }
  106.  
  107. /*
  108.  * align (by extending) _heap_base to BASE byte boundary
  109.  */
  110. EXPORT    char    *_heap_align_base(int base)
  111. {
  112.   int    diff;
  113.   
  114.   diff = (long)_heap_base % base;
  115.   _heap_base += base - diff;
  116.   
  117.   return _heap_alloc(base - diff);
  118. }
  119.